<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Insert title here</title>
        <script src="/lib/axios.js"></script>
        <script src="/lib/vue.js"></script>
        <link rel="stylesheet" href="/lib/elementindex.css">
        <script src="/lib/elementindex.js"></script>
    </head>
    <body>
        <div id="app">
            <el-tabs v-model='currentTab'>
                <el-tab-pane label="基本信息" name="rbasic">
                    <hc-rule-basic :ruledef='ruleDef'></hc-rule-basic>

                    <el-form label-width="100px">
                        <el-form-item label="默认入口规则">
                            <el-cascader style='width: 100%;' :props="{label:'rname',value:'rpath',children:'list',expandTrigger:'hover',emitPath:false}"
                                v-model='ruleDef.defaultEntry' placeholder="关键字搜索" :options="ruleTree" filterable
                                clearable>
                            </el-cascader>
                        </el-form-item>

                        <el-form-item label="">
                            <el-button type="primary" @click='handleUpdate'>保存修改</el-button>
                            &nbsp;&nbsp;
                            <el-button plain type="danger" @click='handleDelete'>删除规则</el-button>
                        </el-form-item>
                    </el-form>
                </el-tab-pane>

                <el-tab-pane label="规则测试" name="test">
                    <hc-test :rpath="ruleDef.rpath" :inputs='ruleDef.rtest' @save-inputs="ruleDef.rtest = $event"></hc-test>
                </el-tab-pane>

                <el-tab-pane label="规则下载" name="download">
                    <el-form>
                        <el-form-item>
                            <el-link type="primary" @click='handleDownloadJson'>下载项目的规则配置</el-link>
                            <!-- <span>可用于备份和恢复</span> -->
                        </el-form-item>
                        <el-form-item>
                            <el-link type="primary" @click='handleDownloadGroovy'>下载项目的规则脚本</el-link>
                        </el-form-item>
                    </el-form>
                </el-tab-pane>
                <el-tab-pane label="同步/发布" name="publish">
                    <el-table :indent="20" :data="adminConfig.remoteList" style="width: 100%;margin-bottom: 20px;"
                        row-key="id" border empty-text='请先完成基本设置'>
                        <el-table-column label="远程服务地址" prop="host" sortable>
                            <template slot-scope="scope">
                                <el-input v-model='scope.row.host'></el-input>
                            </template>
                        </el-table-column>
                        <el-table-column label="appKey" prop="appKey">
                            <template slot-scope="scope">
                                <el-input v-model='scope.row.appKey'></el-input>
                            </template>
                        </el-table-column>
                        <el-table-column label="appSecret" prop="appSecret">
                            <template slot-scope="scope">
                                <el-input type='password' v-model='scope.row.appSecret'></el-input>
                            </template>
                        </el-table-column>
                    </el-table>
                    <el-form>
                        <el-form-item>
                            <el-button type="primary" @click='handleCompare'>与远程仓库对比</el-button>
                            <el-button type="success" :disabled="disablePublish" @click='handlePublishProj'>发布到远程仓库</el-button>
                        </el-form-item>
                    </el-form>

                    <el-table v-if='diffResult.length>0' :indent="20" :data="diffResult" style="width: 100%;margin-bottom: 20px;"
                        row-key="id" border empty-text='' default-expand-all :tree-props="{children: 'list', hasChildren: 'hasChildren'}">
                        <el-table-column label="结果">
                            <template slot-scope="scope">
                                <span>{{diffTypeMap[scope.row.type]}}</span>
                            </template>
                        </el-table-column>
                        <el-table-column label="内容" prop="name">
                        </el-table-column>
                        <el-table-column label="当前最后更新" prop="self.updatedTime">
                        </el-table-column>
                        <el-table-column label="远程最后更新" prop="other.updatedTime">
                        </el-table-column>
                        <el-table-column label="备注" prop="msg">

                        </el-table-column>
                    </el-table>
                </el-tab-pane>
            </el-tabs>
        </div>

        <script src="/lib/ace-noconflict/ace.js"></script>
        <link rel="stylesheet" href="/com/hc-lang-editor.css" />
        <script type='module'>
            import * as $util from '/lib/hc-util.js'
            import * as $rest from '/lib/hc-rest-service.js'
            import {} from '/com/hc-test.js'
            import {} from '/com/hc-rule-basic-properties.js'
            
            $rest.obtainRuleDefByRpath($util.getUrlArgOrDefault('rpath', 'xsxdjc/Main'), 
                function(ruleDef) {
                    window.vm = new Vue({
                            el: '#app',
                            data: {
                                ruleDef: ruleDef,
                                fileList:[],
                                adminConfig :{},
                                diffResult: [],
                                disablePublish:true,
                                diffTypeMap:{},
                                currentTab:'rbasic',
                                ruleTree: [],
                            },
                            methods: {
                                handleCompare()
                                {
                                    let self = this;
                                    let remote = this.adminConfig.remoteList[0];
                                    remote = Object.assign({rpath:this.ruleDef.rpath},remote)
                                    $rest.postdata('/manage/compareProj',remote,(data)=>{
                                        // console.log('compareProj',data);
                                        vm.diffResult = [data];
                                        let maxid = 0;
                                        $util.$tree.traverse(vm.diffResult,
                                            it=>{
                                                it.id= ++maxid;
                                                it.name = it.self?it.self.rname:it.other.rame;
                                                it.self =  it.self || {}
                                                it.other =  it.other || {}
                                            });
                                        self.disablePublish = vm.diffResult[0].type === 'identical'
                                    })
                                },
                                handlePublishProj()
                                {
                                    let remote = this.adminConfig.remoteList[0];
                                    remote = Object.assign({rpath:this.ruleDef.rpath},remote)
                                    $rest.postdata('/manage/publishProj',remote,(data)=>{
                                        vm.$message({type:'success',message:"发布成功"})
                                        this.handleCompare()
                                    })
                                },
                                handleUpdate: function() {
                                    $rest.post('/manage/updateProject', vm.ruleDef)
                                        .then(function(response) {
                                            let ret = response.data;
                                            if (ret.error) {
                                                vm.$message({
                                                    message: ret.data,
                                                    type: 'warning'
                                                });
                                                return;
                                            } else {
                                                vm.$message({
                                                    message: '保存成功！',
                                                    type: 'success'
                                                });
                                            };
                                            vm.ruleDef = response.data.data;
                                        })
                                        .catch(function(error) {
                                            vm.$message({
                                                message: error,
                                                type: 'warning'
                                            });
                                    
                                        });
                                },
                                handleDelete: function() {
                                    let ruleDef = vm.ruleDef;
                                    let withvm = window.top.vm ? window.top.vm : vm;
                                    withvm.$confirm('此操作将永久删除该决策组件的全部内容, 是否继续?', '提示', {
                                        cancelButtonText: '取消',
                                        confirmButtonText: '确定',
                                        type: 'warning',
                                        center: true
                                    }).then(
                                        () => {
                                            doDelete();
                                        }
                                    ).catch(
                                        () => {}
                                    );
                                    
                                    function doDelete() {
                                        $rest.post('/manage/deleteProject?rpath=' + ruleDef.rpath)
                                            .then(function(response) {
                                                let ret = response.data;
                                    
                                                if (ret.error) {
                                                    withvm.$message({
                                                        message: ret.data,
                                                        type: 'warning'
                                                    });
                                                    return;
                                                } else {
                                                    withvm.$message({
                                                        message: '删除成功！',
                                                        type: 'success'
                                                    });
                                                }
                                                // close tab or forward to welcome page
                                                if (window.top.$workbench) {
                                                    window.top.$workbench.closePage('rpath@' + ruleDef.rpath);
                                                    window.top.$workbench.refreshPage('rtype@' + ruleDef.rtype);
                                                } else document.location.href = webhost + '/welcome.htm';
                                    
                                            })
                                            .catch(function(error) {
                                                withvm.$message({
                                                    message: error,
                                                    type: 'warning'
                                                });
                                            });
                                    }
                                },
                                handleDownloadJson:function()
                                {
                                    let self = this;
                                    $rest.loadProject(this.ruleDef.rpath,function(data){
                                        $util.download(JSON.stringify(data,null,'\t'),self.ruleDef.rpath.split('/').join('.')+'.config');
                                    })
                                },
                                handleDownloadGroovy:function()
                                {
                                    let self = this;
                                    $rest.getdata("/manage/groovy?rpath="+this.ruleDef.rpath,function(data){
                                        $util.download(data,self.ruleDef.rpath.split('/').join('.')+'.groovy');
                                    })
                                },
                                
                            }
                        });
                        
                    $rest.getdata('/admin/load',(data)=>{
                        vm.adminConfig = data;
                    })
                    
                    $rest.check$configOrLoad(
                        it=>{
                            vm.diffTypeMap = it.diffTypeMap;
                            
                            $rest.loadProject(vm.ruleDef.rpath,
                                function(projDef){
                                    vm.ruleTree = $util.ruleTree(projDef);
                                });
                        }
                    );
                    
                    
                });

            
        </script>
    </body>

</html>
